import path  from 'path'
import fs from 'fs'
const reg = /"version": "([.0-9]+)"/
const dealVersion = (val) => {
    const list = val.split('.').map((it) => parseInt(it))
    for (let i = list.length - 1; i > -1; i--) {
        const cu = list[i]
        if (i == list.length - 1) {
            list[i] = cu + 1
        }
        if (list[i] > 9 && i != 0) {
            list[i - 1] = list[i - 1] + 1
            list[i] = 0
        }
    }
    return list.join('.')
}
const changeFile = () => {
    const filePath = path.join(process.cwd(), 'package.json')
    let file = fs.readFileSync(filePath, 'utf-8')
    const temp = file.replace(reg, ($1, $2) => {
        const version = dealVersion($2)
        return `"version": "${version}"`
    })
    fs.writeFileSync(filePath, temp)
}

changeFile()
